package io.babystar.modules.business.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.babystar.common.utils.PageUtils;
import io.babystar.common.utils.R;
import io.babystar.common.utils.ShiroUtils;
import io.babystar.modules.business.entity.StudentManageEntity;
import io.babystar.modules.business.entity.StudentManageFollowEntity;
import io.babystar.modules.business.service.StudentManageFollowService;
import io.babystar.modules.business.service.StudentManageService;
import io.babystar.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 预约跟进表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-05-25 13:44:26
 */
@RestController
@RequestMapping("generator/studentmanagefollow")
public class StudentManageFollowController {
    @Autowired
    private StudentManageFollowService studentManageFollowService;
    @Autowired
    private StudentManageService studentManageService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:studentmanagefollow:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = studentManageFollowService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("generator:studentmanagefollow:info")
    public R info(@PathVariable("id") Integer id){
		StudentManageFollowEntity studentManageFollow = studentManageFollowService.getById(id);
        StudentManageEntity studentManageEntity = studentManageService.getOne(new QueryWrapper<StudentManageEntity>()
                .eq("id", studentManageFollow.getStudentManageId())
                .eq("del_flag", 0));
        studentManageFollow.setListeningIntentionality(studentManageEntity.getListeningIntentionality());

        return R.ok().put("studentManageFollow", studentManageFollow);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:studentmanagefollow:save")
    public R save(@RequestBody StudentManageFollowEntity studentManageFollow){
        SysUserEntity userEntity = (SysUserEntity) ShiroUtils.getSubject().getPrincipal();
        studentManageFollow.setCreator(userEntity.getUserId().toString());
        studentManageFollowService.save(studentManageFollow);
        //修改试听意向度
        if(null != studentManageFollow.getListeningIntentionality()){
            StudentManageEntity studentManageEntity = new StudentManageEntity();
            studentManageEntity.setId(studentManageFollow.getStudentManageId());
            studentManageEntity.setListeningIntentionality(studentManageFollow.getListeningIntentionality());
            studentManageService.updateById(studentManageEntity);
        }

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:studentmanagefollow:update")
    public R update(@RequestBody StudentManageFollowEntity studentManageFollow){
        SysUserEntity userEntity = (SysUserEntity) ShiroUtils.getSubject().getPrincipal();
        studentManageFollow.setUpdator(userEntity.getUserId().toString());
        studentManageFollow.setUpdateAt(new Date());
        studentManageFollowService.updateById(studentManageFollow);
        //修改试听意向度
        if(null != studentManageFollow.getListeningIntentionality()){
            StudentManageEntity studentManageEntity = new StudentManageEntity();
            studentManageEntity.setId(studentManageFollow.getStudentManageId());
            studentManageEntity.setListeningIntentionality(studentManageFollow.getListeningIntentionality());
            studentManageService.updateById(studentManageEntity);
        }

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:studentmanagefollow:delete")
    public R delete(@RequestBody Integer[] ids){
        studentManageFollowService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
